package com.tianxiao.faas.application.web.corner;

import com.tianxiao.faas.api.param.corner.SearchHistoryResult;
import com.tianxiao.faas.api.param.corner.SearchHistorySaveParam;
import com.tianxiao.faas.application.aop.AuthenticationContextHolder;
import com.tianxiao.faas.biz.service.corner.SearchHistoryService;
import com.tianxiao.faas.common.access.Access;
import com.tianxiao.faas.common.result.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/app/search")
public class SearchHistoryController {

    @Resource
    private SearchHistoryService searchHistoryService;

    @RequestMapping(value = "/history/delete")
    @Access
    public Result<Boolean> delete(Long id) {
        searchHistoryService.delete(id);
        return Result.builder().data(true).success(true);
    }

    @RequestMapping(value = "/history/save")
    @Access
    public Result<Boolean> save(@RequestBody SearchHistorySaveParam param) {
        Long userId = AuthenticationContextHolder.currentContext().getUserId();
        searchHistoryService.save(userId, param.getSearchName(), param.getType());
        return Result.builder().data(true).success(true);
    }

    @RequestMapping(value = "/history/deleteAll")
    @Access
    public Result<Boolean> deleteAll(Integer type) {
        Long userId = AuthenticationContextHolder.currentContext().getUserId();
        searchHistoryService.deleteAll(userId, type);
        return Result.builder().data(true).success(true);
    }

    @RequestMapping(value = "/history/list")
    @Access
    public Result<List<SearchHistoryResult>> list(Integer type) {
        Long userId = AuthenticationContextHolder.currentContext().getUserId();
        List<SearchHistoryResult> results = searchHistoryService.list(userId, type);
        return Result.builder().success(true).data(results);
    }


}
